package cn.edu.nju.software.zp13.core.validator;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

/**
 * This class is to validate a xml file
 * @author Ivan
 *
 */
public class XmlValidator {
	private final static SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
	
	/**
	 * 
	 * @param schemaFilePath
	 * @param xmlFilePath
	 */
	public static void test(String schemaFilePath,String xmlFilePath){
		try {
			Schema schema=schemaFactory.newSchema(new File(schemaFilePath));
			Validator validator=schema.newValidator();
			validator.setErrorHandler(new XmlErrorHandler());
			validator.validate(new StreamSource(new File(xmlFilePath)));
			System.out.println("no errors found in "+xmlFilePath);
		} catch (SAXException | IOException e) {
			e.printStackTrace();
		}
		
	}
}
